package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    // 权限新增
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
        } catch (Exception e) {
           return new JsonResult(false, "权限加载失败");
        }
        return new JsonResult(true, "权限加载成功");
    }


    // 权限新增
    @RequestMapping("/save")
    public String save(Permission permission){
        permissionService.save(permission);
        return "redirect:/permission/list.do"; // localhost/permission/list.do
    }


    /*// 去新增页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if(id != null){
            Permission permission = permissionService.get(id);
            model.addAttribute("permission", permission);
        }
        return "permission/input"; // /WEB-INF/views/permission/input.jsp
    }*/

    // 删除权限数据
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"permission:delete","权限删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        try{
            permissionService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除权限操作失败");
        }
        return new JsonResult(true,"删除权限操作成功"); // localhost/permission/list.do
    }

    // 查询所有权限数据
    @RequestMapping("/list")
    //@RequiresPermissions(value={"permission:list","权限列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo query = permissionService.query(qo);
        model.addAttribute("pageResult", query);
        return "permission/list";
    }
}
